package com.caifu.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.caifu.bean.Result;
import com.caifu.bean.ResultUtil;
import com.caifu.mapper.*;
import com.caifu.pojo.*;
import com.caifu.util.AesMyUtil;
import com.caifu.util.DES;
import com.caifu.util.Util;
import com.caifu.vo.UserinfoVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.*;

/**
 * @author tianwenwen
 * ClassName: BmActivityManagerService.java
 * version: 1.0.0
 * createTime: 2020年10月16日 10:59:15
 **/
@Service
@Transactional(rollbackFor = RuntimeException.class)
public class BmActivityManagerService {

    @Resource
    private BmActivityManagerMapper bmActivityManagerMapper;

    @Resource
    private BmTokenMapper bmTokenMapper;

    @Resource
    private BmmUserinfoMapper bmmUserinfoMapper;

    @Resource
    private CouponsRecordMapper couponsRecordMapper;

    @Resource
    private UserInfoMapper userInfoMapper;

    @Resource
    private SysOrganizationMapper sysOrganizationMapper;

    @Resource
    private BmCardMapper bmCardMapper;


    /**
     * description: 获取活动室列表
     * param param
     * return: java.util.List<com.caifu.pojo.BmActivityManager>
     * author: Tian
     * time: 2020/10/16 11:47
     */
    public IPage<BmActivityManager> getActivityList(String param, String currentPage, String pageSize, SysUser user) throws Exception {

        //获取当前管理员组织架构
        HashSet<Long> sysUserOrgId = getSysUserOrgId(user);

        QueryWrapper<BmActivityManager> queryWrapper = new QueryWrapper<>();
        if (Util.isNotNull(param)) {
            queryWrapper.like("name", param).or().like("contacts", param);
        }
        //queryWrapper.eq("status", 1).in("org_id", sysUserOrgId).orderByDesc("created_time");
        queryWrapper.eq("status", 1).orderByDesc("created_time");
        Page<BmActivityManager> page = new Page<>(Long.parseLong(currentPage), Long.parseLong(pageSize));
        IPage<BmActivityManager> bmActivityManagerIPage = bmActivityManagerMapper.selectPage(page, queryWrapper);
        List<BmActivityManager> records = bmActivityManagerIPage.getRecords();
        String haircut = "1";
        String sewing = "2";
        for (BmActivityManager record : records) {
            record.setPhone(AesMyUtil.Decrypt(record.getPhone()));
            //活动室类型
            String type = record.getType();
            String[] types = type.split(",");
            if (types.length > 1) {
                record.setType("理发室、缝纫室");
            } else if (haircut.equals(type)) {
                record.setType("理发室");
            } else if (sewing.equals(type)) {
                record.setType("缝纫室");
            }
        }
        return bmActivityManagerIPage;
    }

    /**
     * description: 添加活动室
     * param activityManager
     * return: void
     * author: Tian
     * time: 2020/10/16 13:05
     */
    public Result addActivity(BmActivityManager activityManager) throws Exception {
        if (Util.isNull(activityManager.getId())) {
            //校验手机号是否存在
            if (checkPhone(activityManager.getPhone())) {
                return ResultUtil.error(5050, "手机号已存在");
            }

            //添加
            activityManager.setCreatedTime(new Date());
            activityManager.setUpdatedTime(new Date());
            activityManager.setPhone(AesMyUtil.Encrypt(activityManager.getPhone()));
            activityManager.setStatus("1");
            bmActivityManagerMapper.insert(activityManager);

            //添加token
            BmToken bmToken = new BmToken();
            bmToken.setUpdatedTime(new Date());
            bmToken.setCreatedTime(new Date());
            bmToken.setActivityManagerId(activityManager.getId());
            bmToken.setToken(Util.makeToken());
            bmTokenMapper.insert(bmToken);
            return ResultUtil.success("200", "保存成功");
        } else {
            //修改
            BmActivityManager bmActivityManager = bmActivityManagerMapper.selectById(activityManager.getId());
            //校验手机号是否存在
            if (!AesMyUtil.Encrypt(activityManager.getPhone()).equals(bmActivityManager.getPhone()) && checkPhone(activityManager.getPhone())) {
                return ResultUtil.error(5050, "手机号已存在");
            }
            bmActivityManager.setUpdatedTime(new Date());
            bmActivityManager.setAddress(activityManager.getAddress());
            bmActivityManager.setContacts(activityManager.getContacts());
            bmActivityManager.setName(activityManager.getName());
            bmActivityManager.setOrgId(activityManager.getOrgId());
            bmActivityManager.setPhone(AesMyUtil.Encrypt(activityManager.getPhone()));
            bmActivityManager.setType(activityManager.getType());
            bmActivityManagerMapper.updateById(bmActivityManager);
            return ResultUtil.success("200", "保存成功");
        }
    }

    /**
     * description: 根据id获取活动室
     * param id
     * return: com.caifu.pojo.BmActivityManager
     * author: Tian
     * time: 2020/10/16 13:14
     */
    public BmActivityManager getActivityById(String id) throws Exception {
        BmActivityManager bmActivityManager = bmActivityManagerMapper.selectById(id);
        bmActivityManager.setPhone(AesMyUtil.Decrypt(bmActivityManager.getPhone()));
        String[] orgs = bmActivityManager.getOrgId().split(",");
        bmActivityManager.setOrgIds(orgs);
        return bmActivityManager;
    }


    /**
     * description: 根据id删除活动室
     * param id
     * return: void
     * author: Tian
     * time: 2020/10/16 13:19
     */
    public void deleteActivityById(String id) {
        BmActivityManager bmActivityManager = bmActivityManagerMapper.selectById(id);
        bmActivityManager.setStatus("9");
        bmActivityManager.setUpdatedTime(new Date());
        bmActivityManagerMapper.updateById(bmActivityManager);
    }

    /**
     * description: 校验手机号是否存在
     * param phone
     * return: boolean
     * author: Tian
     * time: 2020/10/21 14:37
     */
    private boolean checkPhone(String phone) throws Exception {
        boolean flag = false;
        QueryWrapper<BmActivityManager> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("phone", AesMyUtil.Encrypt(phone)).eq("status", 1);
        int count = bmActivityManagerMapper.selectCount(queryWrapper);
        if (count > 0) {
            flag = true;
        }
        return flag;
    }

    /**
     * description: 终端机登录
     * param phone request
     * return: java.util.Map<java.lang.String,java.lang.Object>
     * author: Tian
     * time: 2020/10/21 14:37
     */
    public Map<String, Object> bmActivityInfo(String cardId) throws Exception {
        //将设备号添加到session
        Map<String, Object> map = new HashMap<>(15);
        QueryWrapper<BmActivityManager> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("card_id", cardId).eq("status", 1);
        BmActivityManager bmActivityManager = bmActivityManagerMapper.selectOne(queryWrapper);
        if (Util.isNull(bmActivityManager)) {
            map.put("code", "404");
            map.put("msg", "此设备暂未绑定活动室");
            return map;
        }
        String type = bmActivityManager.getType();
        String[] types = type.split(",");
        String lifa = "1";
        String fengren = "2";
        ArrayList<Map<String, Object>> maps = new ArrayList<>();
        Map<String, Object> map1;
        for (String s : types) {
            map1 = new HashMap<>(15);
            if (lifa.equals(s)) {
                map1.put("name", "理发馆");
                map1.put("type", "1");
            } else if (fengren.equals(s)) {
                map1.put("name", "缝纫馆");
                map1.put("type", "2");
            }
            maps.add(map1);
        }
        map.put("code", 200);
        map.put("msg", "请求成功");
        map.put("types", maps);
        map.put("status", bmActivityManager.getStatus());
        return map;
    }

    /**
     * description: 根据身份证获取老人信息
     * param userPid tokenId request
     * return: com.caifu.bean.Result
     * author: Tian
     * time: 2020/10/21 14:38
     */
    public Map<String, Object> getUserinfoByUserPid(String type, String userPid, String cardId) throws Exception {

        System.out.println("type = " + type);
        System.out.println("userPid = " + userPid);
        Map<String, Object> map = new HashMap<>(10);
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_pid", AesMyUtil.Encrypt(DES.decryptDES(userPid))).eq("status", 1);
        UserInfo userInfo = userInfoMapper.selectOne(queryWrapper);
        UserinfoVo userinfoVo = new UserinfoVo();
        //查询活动室服务范围
        QueryWrapper<BmActivityManager> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("card_id", cardId).eq("status", 1);
        BmActivityManager bmActivityManager = bmActivityManagerMapper.selectOne(queryWrapper2);
        String[] orgIds = bmActivityManager.getOrgId().split(",");
        //判断是否是安亭镇老人或者在不在服务范围
        if (Util.isNull(userInfo) || !Arrays.asList(orgIds).contains(userInfo.getBelongOrganization())) {
            map.put("code", "404");
            map.put("msg", "您不是安亭镇老人或不在设备服务范围");
            return map;
        } else {
            String male = "1";
            String female = "2";
            userinfoVo.setAge(userInfo.getAge());
            userinfoVo.setImgPath(userInfo.getImgPath());
            userinfoVo.setUserName(userInfo.getUserName());
            userinfoVo.setUserPid(userInfo.getUserPid());
            if (male.equals(userInfo.getUserSex())) {
                userinfoVo.setUserSex("男");
            } else if (female.equals(userInfo.getUserSex())) {
                userinfoVo.setUserSex("女");
            } else {
                userinfoVo.setUserSex("未知");
            }

            //查询老人优惠券
            QueryWrapper<BmUserinfo> userinfoQueryWrapper = new QueryWrapper<>();
            userinfoQueryWrapper.eq("userinfo_id", userInfo.getUserNo()).eq("type", type).eq("status", 1);
            BmUserinfo bmUserinfo = bmmUserinfoMapper.selectOne(userinfoQueryWrapper);

            System.out.println("测试测试 = " + bmUserinfo);

            if (Util.isNotNull(bmUserinfo)) {
                userinfoVo.setUnusedCoupons(bmUserinfo.getUnusedCoupons());
            } else {
                userinfoVo.setUnusedCoupons(0);
            }
        }
        map.put("code", "200");
        map.put("msg", "suceess");
        map.put("data", userinfoVo);
        return map;
    }

    /**
     * description: 核销使用券
     * param type userPid tokenId request
     * return: com.caifu.bean.Result
     * author: Tian
     * time: 2020/10/21 14:38
     */
    public Result pinCoupon(String type, String userPid, String cardId) {

        //根据身份证查询老人信息
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_pid", userPid).eq("status", 1);
        UserInfo userInfo = userInfoMapper.selectOne(queryWrapper);
        //查询老人优惠信息表老人
        if (Util.isNotNull(userInfo)) {
            QueryWrapper<BmUserinfo> bmUserinfoQueryWrapper = new QueryWrapper<>();
            bmUserinfoQueryWrapper.eq("userinfo_id", userInfo.getUserNo()).eq("type", type).eq("status", 1);
            BmUserinfo bmUserinfo = bmmUserinfoMapper.selectOne(bmUserinfoQueryWrapper);
            if (bmUserinfo.getUnusedCoupons() == 0) {
                return ResultUtil.error(500, "优惠券已为零");
            }
            //将老人券减去一
            int unUsecoups = bmUserinfo.getUnusedCoupons() - 1;
            bmUserinfo.setUnusedCoupons(unUsecoups);
            bmUserinfo.setUpdatedTime(new Date());
            //已使用券数加一
            int use = bmUserinfo.getUsedCoupons() + 1;
            bmUserinfo.setUsedCoupons(use);
            bmmUserinfoMapper.updateById(bmUserinfo);

            //添加老人优惠券使用记录
            BmCouponsRecord bmCouponsRecord = new BmCouponsRecord();
            bmCouponsRecord.setCreatedTime(new Date());
            bmCouponsRecord.setUpdatedTime(new Date());
            bmCouponsRecord.setNumber("1");
            bmCouponsRecord.setHairCut("2");
            bmCouponsRecord.setType(type);
            //获取消费优惠券地点
            QueryWrapper<BmActivityManager> bmActivityManagerQueryWrapper = new QueryWrapper<>();
            bmActivityManagerQueryWrapper.eq("card_id", cardId).eq("status", 1);
            BmActivityManager bmActivityManager = bmActivityManagerMapper.selectOne(bmActivityManagerQueryWrapper);
            bmCouponsRecord.setSourceUse(bmActivityManager.getName());
            bmCouponsRecord.setBmUserinfoId(bmUserinfo.getId());
            bmCouponsRecord.setActivityId(bmActivityManager.getId());
            couponsRecordMapper.insert(bmCouponsRecord);
        }
        return ResultUtil.success(200, "操作成功", "");
    }


    /**
     * 验证token
     */
    private boolean checkToken(String tokenId, String phone) {
        //验证tokenId是否有效
        QueryWrapper<BmActivityManager> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("phone", phone).eq("status", 1);
        BmActivityManager bmActivityManager = bmActivityManagerMapper.selectOne(queryWrapper);
        //查询token
        if (Util.isNotNull(bmActivityManager)) {
            QueryWrapper<BmToken> bmTokenQueryWrapper = new QueryWrapper<>();
            bmTokenQueryWrapper.eq("activity_manager_id", bmActivityManager.getId());
            BmToken bmToken = bmTokenMapper.selectOne(bmTokenQueryWrapper);
            if (tokenId.equals(bmToken.getToken())) {
                return true;
            }
        }
        return false;
    }


    /**
     * 获取当前管理员组织架构
     */
    private HashSet<Long> getSysUserOrgId(SysUser user) throws Exception {
        //查询当前管理员类型为3得节点
        Map<String, Object> param = new HashMap<>();
        param.put("userNo", String.valueOf(user.getUserNo()));
        param.put("type", 3);
        List<Map<String, Object>> orgList = sysOrganizationMapper.selectOrgIdsByUserNoAndType(param);
        HashSet<Long> longs = new HashSet<>();
        for (Map<String, Object> map : orgList) {
            String[] codes = map.get("orgPathCode").toString().split(",");
            for (String code : codes) {
                longs.add(Long.parseLong(code));
            }
        }
        return longs;
    }

    /**
     * description: 获取设备列表
     * param
     * return: java.util.List<com.caifu.pojo.BmCard>
     * author: Tian
     * time: 2020/12/10 11:04
     */
    public List<BmCard> getCrads() {
        //查询已绑定设备号
        List<String> cards = bmActivityManagerMapper.selectCardIds();
        QueryWrapper<BmCard> queryWrapper = new QueryWrapper<>();
        if (cards.size() > 0) {
            queryWrapper.notIn("card_id", cards);
        }
        return bmCardMapper.selectList(queryWrapper);
    }

}
